﻿using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using YoucaiReader.ImageTool;

namespace YoucaiReader.Helper.Drawing
{
    public sealed class ImageImpl : Image
    {
        private Dimension dimension;
        private bool fetched = true;
        private int format;
        private sbyte[] imageData;
        private Dimension imageDimension;
        private ImageSource imageSource;

        public ImageImpl(sbyte[] imageData, Dimension maxDimension, int sourceType)
        {
            if (((maxDimension.height == 0) || (maxDimension.width == 0)) || (imageData == null))
            {
                throw new ArgumentException("Image creation failed because either the dimensions were 0 or the data was null");
            }
            this.imageData = imageData;
            this.format = ImageTools.getImageInfo(imageData, out this.imageDimension);
            if ((this.imageDimension.height == 0) || (this.imageDimension.width == 0))
            {
                throw new ArgumentException("Dimensions could not be determined for the provided image data");
            }
            if (0x63 == this.format)
            {
                throw new ArgumentException("Unexpected image format");
            }
            if ((maxDimension.height != -1) || (maxDimension.width != -1))
            {
                float num = ((float)this.imageDimension.height) / ((float)maxDimension.height);
                float num2 = ((float)this.imageDimension.width) / ((float)maxDimension.width);
                float num3 = (((num2 < 0f) || (num < 0f)) || ((num2 < 1f) && (num < 1f))) ? 1f : ((num2 > num) ? num2 : num);
                this.dimension = new Dimension((int)(((float)this.imageDimension.width) / num3), (int)(((float)this.imageDimension.height) / num3));
            }
            else
            {
                this.dimension = new Dimension(this.imageDimension.width, this.imageDimension.height);
            }
            this.dimension.width = Math.Max(this.dimension.width, 1);
            this.dimension.height = Math.Max(this.dimension.height, 1);
        }

        public bool fetch()
        {
            return this.fetched;
        }

        public int getHeight()
        {
            return this.dimension.height;
        }

        public int getWidth()
        {
            return this.dimension.width;
        }

        public bool isFetched()
        {
            return this.fetched;
        }

        public bool isSelectable()
        {
            return false;
        }

        //public Image SilverlightImage
        public System.Windows.Controls.Image SilverlightImage
        {
            get
            {
                //Image image = new Image();
                //image.set_Width((double)this.dimension.width);
                //image.set_Height((double)this.dimension.height);
                //image.set_Source(this.Source);
                //return image;

                System.Windows.Controls.Image image = new System.Windows.Controls.Image()
                {
                    Width = (double)this.dimension.width,
                    Height = (double)this.dimension.height,
                    Source = this.Source
                };
                return image;
                
            }
        }

        public ImageSource Source
        {
            get
            {
                BitmapImage image = null;
                ImageSource source;
                try
                {
                    if (this.imageSource == null)
                    {
                        //using (MemoryStream stream = new MemoryStream((byte[])this.imageData))
                        List<byte> bytes = new List<byte>();
                        foreach (sbyte sb in this.imageData)
                        {
                            bytes.Add(Convert.ToByte(sb));
                        }
                        using(var stream = new MemoryStream(bytes.ToArray()))
                        {
                            switch (this.format)
                            {
                                case 1:
                                    {
                                        int[] src = ImageTools.getARGBfromBMP(this.imageData, this.imageDimension);
                                        WriteableBitmap bitmap = new WriteableBitmap(this.imageDimension.width, this.imageDimension.height);
                                        int[] dst = bitmap.Pixels;
                                        Buffer.BlockCopy(src, 0, dst, 0, dst.Length * 4);
                                        this.imageSource = bitmap;
                                        goto Label_00D5;
                                    }
                                case 2:
                                    {
                                        //Image image2 = new Image
                                        //{
                                        //    IsLoadingSynchronously = true
                                        //};
                                        //image2.SetSource(stream);
                                        //this.imageSource = image2.ToBitmap();

                                        YoucaiReader.ImageTool.Image image2 = new YoucaiReader.ImageTool.Image();
                                        image2.IsLoadingSynchronously = true;
                                        image2.SetSource(stream);
                                        this.imageSource = image2.ToBitmap();
                                        goto Label_00D5;
                                    }
                            }
                            image = new BitmapImage();
                            image.SetSource(stream);
                            this.imageSource = image;
                        }
                    }
                Label_00D5:
                    source = this.imageSource;
                }
                catch (Exception)
                {
                    this.fetched = false;
                    if (image != null)
                    {
                        image.SetSource(null);
                    }
                    throw;
                }
                return source;
            }
        }
    }
}

